using UnityGameFramework.Runtime;

namespace LS.UGFUnits
{
    using Unity.VisualScripting;
    
    public class GetEntity : EntityUnit
    {
        [DoNotSerialize]
        public ValueInput SerialId { get; set; }
        
        [DoNotSerialize]
        [PortLabelHidden]
        public ValueOutput Entity { get; set; }
        
        [DoNotSerialize]
        [PortLabelHidden]
        public ValueOutput Result { get; set; }
        
        protected override void Definition()
        {
            base.Definition();
            SerialId = ValueInput<int>(nameof(SerialId));
            Entity = ValueOutput<Entity>(nameof(Entity));
            Result = ValueOutput<bool>(nameof(Result));
            Requirement(SerialId, Input);
        }

        protected override ControlOutput In(Flow flow)
        {
            int id = flow.GetValue<int>(SerialId);
            var entity = Target?.GetEntity(id) ?? null;
            flow.SetValue(Entity, entity);
            flow.SetValue(Result, entity != null);
            return base.In(flow);
        }
    }
}